<template>
  <div class="c-rank">
    <div class="c-rank-title" v-if="title">{{title}}</div>
    <div class="c-rank-list">
      <div class="c-rank-item" v-for="(item,index) in listB" :key="`c-rank-item-${index}`">
        <div class="c-rank-num">{{index + 1}}</div>
        <div class="c-rank-name single-text-overflow">{{item.name}}</div>
        <div class="c-rank-bar">
          <div class="c-rank-bar-outer">
            <div class="c-rank-bar-inner" :style="{width: item.persent}"></div>
          </div>
        </div>
        <div class="c-rank-count">{{item.count}}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "data-v-rank",
  props: {
    title: {
      type: String,
      default: ""
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      listA: this.list
    };
  },
  watch: {
    list(val) {
      this.listA = val;
    }
  },
  computed: {
    max() {
      return Math.max(...this.list.map(it => it.count));
    },
    listB() {
      return this.listA.map(item => {
        return {
          name: item.name,
          count: item.count,
          persent: (item.count / this.max).toFixed(2) * 100 + "%"
        };
      });
    }
  }
};
</script>

<style>
.c-rank {
  display: flex;
  flex-direction: column;
}
.c-rank-title {
  height: 48px;
  line-height: 48px;
  padding: 0 10px;
  border-bottom: 1px solid #1c2e46;
}
.c-rank-list {
  padding: 0 10px;
  /* margin-right: -20px; */
  flex: 1;
  overflow-y: auto;
}
.c-rank-list::-webkit-scrollbar {
  width: 0 !important;
}
.c-rank-item {
  height: 36px;
  line-height: 36px;
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}
.c-rank-name,
.c-rank-bar {
  flex: 1;
}
.c-rank-name,
.c-rank-num {
  display: flex;
  align-items: center;
}
.c-rank-bar {
  flex-basis: 80%;
  display: flex;
  align-items: center;
  padding: 0 20px;
}
.c-rank-num {
  width: 40px;
  padding-right: 20px;
}
.c-rank-count {
  width: 40px;
  text-align: right;
}
.c-rank-bar-outer {
  display: flex;
  align-items: center;
  width: 100%;
  height: 6px;
  border: 1px solid #5b7999;
  border-radius: 5px;
}
.c-rank-bar-inner {
  margin: 0 2px;
  height: 2px;
  background-color: #8fc31f;
}
/*单行超出隐藏*/
.single-text-overflow {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

</style>
